home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.5)
-
- import os
- import re
- import HTMLParser
- import base64
- import glob
- from os.path import join, expanduser, exists, basename
- from gettext import gettext as _
- from ConfigParser import RawConfigParser
- from xml.dom import minidom
- from deskbar.defs import VERSION
- import gtk
- from deskbar.Watcher import FileWatcher, DirWatcher
- import deskbar
- import deskbar.Indexer as deskbar
- import deskbar.Handler as deskbar
-
- try:
- set
- except NameError:
- from sets import Set as set
-
- from deskbar.BrowserMatch import is_preferred_browser
- from deskbar.BrowserMatch import BrowserSmartMatch, BrowserMatch
- USING_FIREFOX = False
- if is_preferred_browser('firefox'):
- USING_FIREFOX = True
-
-
- def get_mozilla_home_file(needed_file):
- default_profile_dir = expanduser('~/.mozilla/default')
- if exists(default_profile_dir):
- for d in os.listdir(default_profile_dir):
- return join(default_profile_dir, d, needed_file)
-
-
- return ''
-
-
- def get_firefox_home_file(needed_file):
- firefox_dir = expanduser('~/.mozilla/firefox/')
- config = RawConfigParser({
- 'Default': 0 })
- config.read(expanduser(join(firefox_dir, 'profiles.ini')))
- path = None
- for section in config.sections():
- if config.has_option(section, 'Default') and config.get(section, 'Default') == '1':
- path = config.get(section, 'Path')
- break
- continue
- if path == None and config.has_option(section, 'Path'):
- path = config.get(section, 'Path')
- continue
-
- if path == None:
- return ''
-
- if path.startswith('/'):
- return join(path, needed_file)
-
- return join(firefox_dir, path, needed_file)
-
- GCONF_SHOW_ONLY_PRIMARY_KEY = deskbar.GCONF_DIR + '/mozilla/show_only_primary_search'
- SHOW_ONLY_PRIMARY = deskbar.GCONF_CLIENT.get_bool(GCONF_SHOW_ONLY_PRIMARY_KEY)
- if SHOW_ONLY_PRIMARY == None:
- SHOW_ONLY_PRIMARY = False
-
-
- def _on_gconf_show_only_primary(value):
- global SHOW_ONLY_PRIMARY
- SHOW_ONLY_PRIMARY = value
-
- deskbar.GCONF_CLIENT.notify_add(GCONF_SHOW_ONLY_PRIMARY_KEY, (lambda x, y, z, a: _on_gconf_show_only_primary(z.value.get_bool())))
- PRIMARY_SEARCH_ENGINE = None
-
- try:
- if USING_FIREFOX:
- prefs_file = file(get_firefox_home_file('prefs.js'))
- else:
- prefs_file = None
- for line in prefs_file:
- if line.startswith('user_pref("browser.search.selectedEngine", "'):
- line = line.strip()
- PRIMARY_SEARCH_ENGINE = line[len('user_pref("browser.search.selectedEngine", "'):-len('");')]
- break
- continue
- except:
- pass
-
-
- def _on_handler_preferences(dialog):
-
- def toggled_cb(sender, show_all_radio, show_primary_radio):
- deskbar.GCONF_CLIENT.set_bool(GCONF_SHOW_ONLY_PRIMARY_KEY, show_primary_radio.get_active())
-
-
- def sync_ui(new_show_only_primary, show_all_radio, show_primary_radio):
- show_all_radio.set_active(not new_show_only_primary)
- show_primary_radio.set_active(new_show_only_primary)
-
- glade = gtk.glade.XML(os.path.join(deskbar.SHARED_DATA_DIR, 'mozilla-search.glade'))
- dialog = glade.get_widget('prefs-dialog')
- show_all_radio = glade.get_widget('show_all_radio')
- show_primary_radio = glade.get_widget('show_primary_radio')
- show_primary_radio.set_active(SHOW_ONLY_PRIMARY)
- show_all_radio.set_active(not SHOW_ONLY_PRIMARY)
- show_all_radio.connect('toggled', toggled_cb, show_all_radio, show_primary_radio)
- show_primary_radio.connect('toggled', toggled_cb, show_all_radio, show_primary_radio)
- notify_id = None((None, deskbar.GCONF_CLIENT.notify_add, GCONF_SHOW_ONLY_PRIMARY_KEY), (lambda x, y, z, a: sync_ui(z.value.get_bool(), show_all_radio, show_primary_radio)))
- dialog.set_icon_name('deskbar-applet')
- dialog.show_all()
- dialog.run()
- dialog.destroy()
- deskbar.GCONF_CLIENT.notify_remove(notify_id)
-
-
- def _check_requirements_bookmarks():
- if is_preferred_browser('firefox') or is_preferred_browser('mozilla'):
- return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
- else:
- return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, 'Mozilla/Firefox is not your preferred browser, not using it.', None)
-
-
- def _check_requirements_search():
- if is_preferred_browser('firefox'):
- return (deskbar.Handler.HANDLER_IS_CONFIGURABLE, _('You can customize which search engines are offered.'), _on_handler_preferences)
- elif is_preferred_browser('mozilla'):
- return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
- else:
- return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, 'Mozilla/Firefox is not your preferred browser, not using it.', None)
-
- HANDLERS = {
- 'MozillaBookmarksHandler': {
- 'name': _('Web Bookmarks'),
- 'description': _('Open your web bookmarks by name'),
- 'requirements': _check_requirements_bookmarks,
- 'version': VERSION },
- 'MozillaSearchHandler': {
- 'name': _('Web Searches'),
- 'description': _("Search the web via your browser's search settings"),
- 'requirements': _check_requirements_search,
- 'version': VERSION },
- 'MozillaHistoryHandler': {
- 'name': _('Web History'),
- 'description': _('Open your web history by name'),
- 'requirements': _check_requirements_bookmarks,
- 'version': VERSION } }
-
- class MozillaBookmarksHandler(deskbar.Handler.Handler):
-
- def __init__(self):
- deskbar.Handler.Handler.__init__(self, 'stock_bookmark')
- self._bookmarks = None
-
-
- def initialize(self):
- if not hasattr(self, 'watcher'):
- self.watcher = FileWatcher()
- self.watcher.connect(('changed',), (lambda watcher, f: self._parse_bookmarks()))
-
- parsed_file = self._parse_bookmarks()
- if parsed_file != None:
- self.watcher.add(parsed_file)
-
-
-
- def _parse_bookmarks(self):
- (self._bookmarks, parsed_file, self._shortcuts_to_smart_bookmarks_map) = MozillaBookmarksParser(self).get_indexer()
- return parsed_file
-
-
- def stop(self):
- self.watcher.remove_all()
-
-
- def query(self, query):
- x = self.query_smart_bookmarks(query, deskbar.DEFAULT_RESULTS_PER_HANDLER)
- if x != None:
- return x
- else:
- return self._bookmarks.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
-
-
- def query_smart_bookmarks(self, query, max):
- x = query.find(' ')
- if x != -1:
- prefix = query[:x]
-
- try:
- b = self._shortcuts_to_smart_bookmarks_map[prefix]
- text = query[x + 1:]
- return [
- BrowserSmartMatch(b.get_handler(), b.name, b.url, prefix, b, icon = b.icon)]
- except KeyError:
- pass
- except:
- None<EXCEPTION MATCH>KeyError
-
-
- None<EXCEPTION MATCH>KeyError
-
-
-
- class MozillaSearchHandler(deskbar.Handler.Handler):
-
- def __init__(self):
- deskbar.Handler.Handler.__init__(self, 'stock_bookmark')
- self._smart_bookmarks = None
-
-
- def initialize(self):
- smart_dirs = None
- if USING_FIREFOX:
- smart_dirs = [
- get_firefox_home_file('searchplugins'),
- get_firefox_home_file('search'),
- expanduser('~/.mozilla/searchplugins'),
- '/usr/lib/firefox/searchplugins',
- '/usr/local/lib/firefox/searchplugins',
- '/usr/lib/mozilla-firefox/searchplugins',
- '/usr/local/lib/mozilla-firefox/searchplugins']
- else:
- smart_dirs = [
- get_mozilla_home_file('search'),
- expanduser('~/.mozilla/searchplugins'),
- '/usr/lib/mozilla/searchplugins',
- '/usr/local/lib/mozilla/searchplugins']
- if not hasattr(self, 'watcher'):
- self.watcher = DirWatcher()
- None((self.watcher.connect, 'changed'), (lambda watcher, f: self._parse_search_engines(smart_dirs)))
-
- self.watcher.add(smart_dirs)
- self._parse_search_engines(smart_dirs)
-
-
- def _parse_search_engines(self, smart_dirs):
- self._smart_bookmarks = MozillaSmartBookmarksDirParser(self, smart_dirs).get_smart_bookmarks()
-
-
- def stop(self):
- self.watcher.remove_all()
-
-
- def query(self, query):
- if SHOW_ONLY_PRIMARY and PRIMARY_SEARCH_ENGINE != None:
- for s in self._smart_bookmarks:
- if s.name == PRIMARY_SEARCH_ENGINE:
- return [
- s]
- continue
-
- return self._smart_bookmarks
- else:
- return self._smart_bookmarks
-
-
-
- class MozillaBookmarksParser(HTMLParser.HTMLParser):
-
- def __init__(self, handler):
- HTMLParser.HTMLParser.__init__(self)
- self.handler = handler
- self.chars = ''
- self.href = None
- self.icon_data = None
- self.bookmarks = set()
- self._shortcuts_to_smart_bookmarks_map = { }
- self._indexer = deskbar.Indexer.Indexer()
-
- try:
- if USING_FIREFOX:
- self.indexed_file = self._index_firefox()
- else:
- self.indexed_file = self._index_mozilla()
- self.close()
- except Exception:
- e = None
- print 'Warning:Could not index Firefox bookmarks:', e
-
-
-
- def get_indexer(self):
- '''
- \t\tReturns a completed indexer with the contents of bookmark file,
- \t\tthe name of the indexed file, and a map from shortcuts (or
- \t\tprefixes) to smart bookmarks - those bookmarks with %s in the
- \t\tURL.
- \t\t'''
- return (self._indexer, self.indexed_file, self._shortcuts_to_smart_bookmarks_map)
-
-
- def _index_mozilla(self):
-
- try:
- bookmarks_file = get_mozilla_home_file('bookmarks.html')
- if exists(bookmarks_file):
- self.feed(file(bookmarks_file).read())
- return bookmarks_file
- except Exception:
- msg = None
- print 'Error retreiving Mozilla Bookmarks:', msg
-
-
-
- def _index_firefox(self):
-
- try:
- bookmarks_file = get_firefox_home_file('bookmarks.html')
- if exists(bookmarks_file):
- self.feed(file(bookmarks_file).read())
- return bookmarks_file
- except Exception:
- msg = None
- print 'Error retreiving Mozilla Bookmarks:', msg
-
-
-
- def handle_starttag(self, tag, attrs):
- tag = tag.lower()
- if tag == 'a':
- self.chars = ''
- self.shortcuturl = None
- for tag, value in attrs:
- if tag.lower() == 'href':
- self.href = value
-
- if tag.lower() == 'icon' and value != 'data:' and value.startswith('data:'):
- self.icon_data = value
-
- if tag.lower() == 'shortcuturl':
- self.shortcuturl = value
- continue
-
-
-
-
- def handle_endtag(self, tag):
- tag = tag.lower()
- if tag == 'a':
- if self.href == None or self.href.startswith('javascript:'):
- return None
-
- pixbuf = None
- if self.icon_data != None:
-
- try:
- (header, content) = self.icon_data.split(',', 2)
- loader = gtk.gdk.PixbufLoader()
- loader.set_size(deskbar.ICON_HEIGHT, deskbar.ICON_HEIGHT)
-
- try:
- loader.write(base64.b64decode(content))
- except AttributeError:
- loader.write(base64.decodestring(content))
-
- loader.close()
- pixbuf = loader.get_pixbuf()
- except Exception:
- msg = None
- print 'Error:mozilla.py:handle_endtag:', msg
-
- self.icon_data = None
-
- bookmark = BrowserMatch(self.handler, self.chars, self.href, icon = pixbuf)
- if self.shortcuturl != None:
- bookmark = BrowserSmartMatch(self.handler, self.chars, self.href, self.shortcuturl, bookmark, icon = pixbuf)
- self._shortcuts_to_smart_bookmarks_map[self.shortcuturl] = bookmark
- else:
- self._indexer.add('%s %s' % (self.chars, self.href), bookmark)
-
-
-
- def handle_data(self, chars):
- self.chars = self.chars + chars
-
-
-
- class Firefox2SearchEngineParser:
-
- def __init__(self, filename):
- self.filename = filename
- self._infos = {
- 'name': '',
- 'action': '',
- 'description': '',
- 'url': '' }
- self._namespace = None
-
-
- def get_infos(self):
- return self._infos
-
-
- def parse(self):
- xml = minidom.parse(self.filename)
- self._detect_namespace(xml)
- self._parse_name(xml)
- self._parse_description(xml)
- self._parse_action_and_url(xml)
-
- try:
- self._parse_image(xml)
- except Exception:
- msg = None
- print 'Error parsing icon for %s\n%s' % (self.filename, msg)
-
-
-
- def _detect_namespace(self, xml):
-
- try:
- plugin = xml.getElementsByTagName('SearchPlugin')[0]
- ns = plugin.getAttribute('xmlns:os')
- if ns == '':
- return None
-
- self._namespace = 'os'
- except:
- pass
-
-
-
- def _ns_convert(self, tagname):
- if self._namespace is not None:
- return self._namespace + ':' + tagname
- else:
- return tagname
-
-
- def _parse_name(self, xml):
- for node in xml.getElementsByTagName(self._ns_convert('ShortName'))[0].childNodes:
- self._infos['name'] += node.data
-
-
-
- def _parse_description(self, xml):
- for node in xml.getElementsByTagName(self._ns_convert('Description'))[0].childNodes:
- self._infos['description'] += node.data
-
-
-
- def _parse_action_and_url(self, xml):
- url_node = None
- for node in xml.getElementsByTagName(self._ns_convert('Url')):
- if node.getAttribute('type') == 'text/html':
- url_node = node
- break
- continue
-
- if url_node is None:
- raise ParseException('No Url tag of type text/html in %s' % self.filename)
-
- self._infos['url'] = url_node.getAttribute('template')
- self._infos['action'] = self._infos['url']
- params = ''
- for param in url_node.getElementsByTagName(self._ns_convert('Param')):
- key = param.getAttribute('name')
- value = param.getAttribute('value')
- params += '&%s=%s' % (key, value)
-
- params = params[1:]
- if params != '':
- self._infos['action'] += '?' + params
-
- self._infos['action'] = self._infos['action'].replace('{searchTerms}', '%s')
- self._infos['action'] = self._infos['action'].replace('{SearchTerms}', '%s')
-
-
- def _parse_image(self, xml):
-
- try:
- img_tag = xml.getElementsByTagName(self._ns_convert('Image'))[0]
- except:
- return None
-
- loader = gtk.gdk.PixbufLoader()
- loader.set_size(deskbar.ICON_HEIGHT, deskbar.ICON_HEIGHT)
- content = ''
- for data in img_tag.childNodes:
- content += data.data
-
- header = 'data:image/x-icon;base64,'
- content = content[content.index(header) + len(header):]
-
- try:
- loader.write(base64.b64decode(content))
- except AttributeError:
- loader.write(base64.decodestring(content))
-
- loader.close()
- pixbuf = loader.get_pixbuf()
- self._infos['pixbuf'] = pixbuf
-
-
-
- class MozillaSmartBookmarksParser:
-
- def __init__(self, f):
- '''
- \t\tInit the parse, no exception here
- \t\t'''
- self.f = f
- self.infos = {
- 'search': { },
- 'input': { } }
-
-
- def get_infos(self):
- infos = { }
- args = '?'
- for key, arg in self.infos['input'].items():
- args += '%s=%s&' % (key, arg)
-
- if args.endswith('&'):
- args = args[:-1]
-
- infos['name'] = self.infos['search']['name']
- infos['description'] = self.infos['search']['description']
- if 'url' not in self.infos['search']:
- infos['url'] = self.infos['search']['action']
- else:
- infos['url'] = self.infos['search']['url']
- infos['action'] = self.infos['search']['action'] + args
- img = self._find_icon()
- if img is not None:
- infos['icon'] = img
-
- return infos
-
-
- def parse(self):
- '''
- \t\t'''
- tokenizer = Tokenizer(self.f)
- n = None
- state = None
- tokens = [
- tokenizer.get_next_token(),
- tokenizer.get_next_token()]
- while tokens[0] != None:
- (state, n, rest) = self._handle_token(state, tokens)
- if n == None:
- break
-
- tokens = rest
- for i in range(n - len(rest)):
- tokens.append(tokenizer.get_next_token())
-
-
-
- def _find_icon(self):
-
- try:
- parent_dir = self.f[:self.f.rindex('/')]
- return _[1][0]
- except Exception:
- msg = None
- print 'WARNING: Error detecting icon for smart bookmark:%s\n%s' % (self.f, msg)
- return None
-
-
-
- def _handle_token(self, state, tokens):
- if state == None:
- if tokens == [
- '<',
- 'search'] or tokens == [
- '<',
- 'SEARCH']:
- return ('search', 3, [])
- elif state == None:
- raise ParseException('File %s does not begin with <search' % self.f)
-
- if state == 'search' and tokens[1] == '=':
- self.infos['search'][tokens[0]] = tokens[2]
- return ('search', 3, [])
-
- if (state == 'search' or state == 'anotherinput') and tokens == [
- '>',
- '<',
- 'input']:
- return ('input', 6, [])
- elif state == 'search':
- raise ParseException('Expecting <input after <search section in file %s' % self.f)
-
- if state == 'input' and tokens[1] == '=':
- if tokens[3] == 'user':
- self.infos['input'][tokens[2]] = '%s'
- if tokens[4] != '=':
- return ('anotherinput', 3, tokens[4:])
-
- else:
- self.infos['input'][tokens[2]] = tokens[5]
- return ('anotherinput', 3, [])
-
- return (None, None, None)
-
-
-
- class TokenException(Exception):
-
- def __init__(self, msg):
- Exception.__init__(self, msg)
-
-
-
- class ParseException(Exception):
-
- def __init__(self, msg):
- Exception.__init__(self, msg)
-
-
-
- class Tokenizer:
-
- def __init__(self, f):
- '''
- \t\tInit the tokenizer on the given file, may throw an exception.
- \t\t'''
- self.i = 0
- self.f = f
- self.data = file(f).read()
- self.state = 'linestart'
-
-
- def get_next_token(self):
- '''
- \t\tReturns the next token in the file.
- \t\tA token is one of:
- \t\t< = > name
- \t\t
- \t\tReturns None when the end of file is reached and beyond
- \t\t'''
- while self.i < len(self.data):
- char = self.data[self.i]
- if char.isspace() and not (char == '\n'):
- self.i += 1
- continue
-
- if self.state == 'linestart' and char == '#':
- while self.i < len(self.data) and self.data[self.i] != '\n':
- self.i += 1
- continue
- self
- self.i += 1
- continue
-
- next_token = self.read_token()
- if next_token == '\n':
- self.state = 'linestart'
- continue
-
- return next_token
-
-
- def read_token(self):
- '''
- \t\tReturn the token, self.i must point to the first char in this token.
- \t\tself.i is updated to point just after the returned token
- \t\tReturned token is one of:
- \t\t< = >
- name
- \t\t'''
- char = self.data[self.i]
- if char == '<' and char == '=' and char == '>' or char == '\n':
- self.i += 1
- return char
- elif char == '"':
- closing = self.data[self.i + 1:].find('"')
- if closing == -1:
- raise TokenException("Couldn't find a proper closing quote in %s" % self.f)
-
- token = self.data[self.i + 1:self.i + 1 + closing]
- self.i += closing + 2
- return token
- else:
- token = ''
- while self.i < len(self.data) and not self.data[self.i].isspace() and self.data[self.i] not in '=><':
- token += self.data[self.i]
- self.i += 1
- continue
- self
- return token
-
-
-
- class MozillaSmartBookmarksDirParser:
-
- def __init__(self, handler, dirs):
- self._smart_bookmarks = []
- bookmark_names = []
- found_bookmarks = []
- for bookmarks_dir in dirs:
- if not exists(bookmarks_dir):
- continue
-
- for f in glob.glob(join(bookmarks_dir, '*.src')):
- bmname = basename(f)
- if bmname in bookmark_names:
- continue
- continue
- found_bookmarks.append(f)
-
- for f in glob.glob(join(bookmarks_dir, '*.xml')):
- bmname = basename(f)
- if bmname in bookmark_names:
- continue
- continue
- found_bookmarks.append(f)
-
-
- for f in found_bookmarks:
- img = None
- if f.endswith('.xml'):
- parser = Firefox2SearchEngineParser(f)
- else:
- parser = MozillaSmartBookmarksParser(f)
-
- try:
- parser.parse()
- infos = parser.get_infos()
- if infos.has_key('pixbuf'):
- bookmark = BrowserMatch(handler, infos['name'], infos['url'], pixbuf = infos['pixbuf'])
- bookmark = BrowserSmartMatch(handler, infos['name'], infos['action'], pixbuf = infos['pixbuf'], bookmark = bookmark)
- elif infos.has_key('icon'):
- bookmark = BrowserMatch(handler, infos['name'], infos['url'], icon = infos['icon'])
- bookmark = BrowserSmartMatch(handler, infos['name'], infos['action'], icon = infos['icon'], bookmark = bookmark)
- else:
- bookmark = BrowserMatch(handler, infos['name'], infos['url'])
- bookmark = BrowserSmartMatch(handler, infos['name'], infos['action'], bookmark = bookmark)
- self._smart_bookmarks.append(bookmark)
- continue
- except Exception:
- msg = None
- print 'Error:MozillaSmartBookmarksDirParser:cannot parse smart bookmark: %s\n%s' % (f, msg)
- continue
-
-
-
-
-
- def get_smart_bookmarks(self):
- '''
- \t\tReturn a list of MozillaSmartMatch instances representing smart bookmarks
- \t\t'''
- return self._smart_bookmarks
-
-
- MOZILLA_HISTORY_REGEX = re.compile('\\=http[0-9a-zA-Z\\-\\&\\%\\=\\?\\:\\/\\.]*\\)')
-
- class MozillaHistoryHandler(deskbar.Handler.Handler):
-
- def __init__(self):
- deskbar.Handler.Handler.__init__(self, 'epiphany-history.png')
- self._history = None
-
-
- def initialize(self):
- self._indexer = deskbar.Indexer.Indexer()
- self._history = self._parse_history()
- for history_url in self._history:
- history_wo_http = history_url[history_url.find('//') + 2:]
- if history_wo_http.find('www.') == -1:
- history_wo_www = history_wo_http
- else:
- history_wo_www = history_wo_http[history_wo_http.find('www.') + 4:]
- self._indexer.add('%s %s %s' % (history_wo_www, history_wo_http, history_url), BrowserMatch(self, history_wo_www, history_url, True))
-
-
-
- def _parse_history(self):
- if USING_FIREFOX:
- historydat = get_firefox_home_file('history.dat')
- else:
- historydat = get_mozilla_home_file('history.dat')
-
- try:
- historycontents = file(historydat).read()
- historycontents = re.findall(MOZILLA_HISTORY_REGEX, historycontents)
- historycontents = [ x[1:-1] for x in historycontents ]
- return historycontents
- except:
- return ''
-
-
-
- def query(self, query):
- return self._indexer.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
-
-
-